package com.applink.sell_proof.database.table;

import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;

import com.applink.sell_proof.EMarketing;
import com.applink.sell_proof.database.EMarketingContentProvider;
import com.applink.sell_proof.model.ProductImage;

public class ProductImageDBHelper {
	public static final String TABLE_NAME = "product_image_table";

	public static final String PDIM_ID = "ProductInfoID";
	public static final String PDIM_INFO = "ProductInfo";
	public static final String PDIM_DEC = "Description";
	public static final String PDIM_CATEGORY = "SaleCategoryName";
	public static final String PDIM_IMAGE_PATH = "PhysicalPath";
	public static final String PDIM_IMAGE_PATH_LOCAL = "ProductImageLocal";
	public static final String PM_DATETIME_SAVED = "PromotionDateTimeSaved";
	public static final String SQL_EXCUTE = "CREATE TABLE " + TABLE_NAME + "("
			+ PDIM_ID + " integer primary key ," + PDIM_INFO + " text ,"
			+ PDIM_DEC + " text ," + PDIM_CATEGORY + " text ,"
			+ PDIM_IMAGE_PATH + " text ," + PDIM_IMAGE_PATH_LOCAL + " text, "
			+ PM_DATETIME_SAVED + " DATETIME);";

	public static void createProductImage(final ProductImage productImage) {
		new Thread() {
			@Override
			public void run() {
				super.run();
				ContentValues value = new ContentValues();
				ContentResolver resolver = EMarketing.getInstance()
						.getApplicationContext().getContentResolver();
				value.put(PDIM_ID, productImage.getProductInfoID());
				value.put(PDIM_INFO, productImage.getProductInfo());
				value.put(PDIM_DEC, productImage.getDescription());
				value.put(PDIM_CATEGORY, productImage.getSaleCategoryName());
				value.put(PDIM_IMAGE_PATH, productImage.getPhysicalPath());
				value.put(PDIM_IMAGE_PATH_LOCAL, productImage.getLocalPath());
				value.put(PDIM_IMAGE_PATH_LOCAL, productImage.getLocalPath());
				value.put(PM_DATETIME_SAVED, productImage.getDownloadedDate());
				resolver.insert(
						EMarketingContentProvider.PRODUCT_IMAGE_CONTENT_URI,
						value);
			}

		}.start();
	}

	public static ArrayList<ProductImage> getAllProductImage() {
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		Cursor cursor = resolver.query(
				EMarketingContentProvider.PRODUCT_IMAGE_CONTENT_URI, null,
				null, null, PDIM_ID + " DESC ");

		if (cursor != null) {
			ArrayList<ProductImage> productImages = new ArrayList<ProductImage>();
			while (cursor.moveToNext()) {
				ProductImage productImage = new ProductImage();
				productImage.setProductInfoID(cursor.getInt(cursor
						.getColumnIndex(PDIM_ID)));
				productImage.setProductInfo(cursor.getString(cursor
						.getColumnIndex(PDIM_INFO)));
				productImage.setDescription(cursor.getString(cursor
						.getColumnIndex(PDIM_DEC)));
				productImage.setSaleCategoryName(cursor.getString(cursor
						.getColumnIndex(PDIM_CATEGORY)));
				productImage.setPhysicalPath(cursor.getString(cursor
						.getColumnIndex(PDIM_IMAGE_PATH)));
				productImage.setLocalPath(cursor.getString(cursor
						.getColumnIndex(PDIM_IMAGE_PATH_LOCAL)));
				productImages.add(productImage);
			}
			return productImages.size() > 0 ? productImages : null;
		}
		return null;
	}

	public static void updateProductImage(ProductImage productImage) {
		ContentValues value = new ContentValues();
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		value.put(PDIM_ID, productImage.getProductInfoID());
		value.put(PDIM_INFO, productImage.getProductInfo());
		value.put(PDIM_DEC, productImage.getDescription());
		value.put(PDIM_CATEGORY, productImage.getSaleCategoryName());
		value.put(PDIM_IMAGE_PATH, productImage.getPhysicalPath());
		value.put(PDIM_IMAGE_PATH_LOCAL, productImage.getLocalPath());

		resolver.update(EMarketingContentProvider.PRODUCT_IMAGE_CONTENT_URI,
				value, PDIM_ID + "=?",
				new String[] { Long.toString(productImage.getProductInfoID()) });

	}

	public static void deleteProductImage() {
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		resolver.delete(EMarketingContentProvider.PRODUCT_IMAGE_CONTENT_URI,
				null, null);
	}
}
